﻿using MediatR;
using OpenTelemetry.Trace;
using Yuekao.API.Read.Command;
using Yuekao.Domain.API;
using Yuekao.Domain.BaseReposi;
using Yuekao.Domain.Entitys;

namespace Yuekao.API.Read.CommandHandler
{
    public class WorkflowDetinitionShowCommandHandler : IRequestHandler<WorkflowDetinitionShowCommand, APIResult<APIPagging<WorkflowDefinitionModel>>>
    {
        private readonly IBaseRepository<WorkflowDefinitionModel> _repository;
        public WorkflowDetinitionShowCommandHandler(IBaseRepository<WorkflowDefinitionModel> repository)
        {
            _repository = repository;
        }
        /// <summary>
        /// 流程设计列表
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public async Task<APIResult<APIPagging<WorkflowDefinitionModel>>> Handle(WorkflowDetinitionShowCommand request, CancellationToken cancellationToken)
        {
            APIResult<APIPagging<WorkflowDefinitionModel>> result = new APIResult<APIPagging<WorkflowDefinitionModel>>();
            result.Code = APIEnum.Success;
            result.Message = "流程设计列表显示成功";
            var list = _repository.GetAll();
            var totaCount = list.Count();
            var pageCount = (int)Math.Ceiling(totaCount * 1.0 / request.pageSize);
            APIPagging<WorkflowDefinitionModel> pagging = new APIPagging<WorkflowDefinitionModel>();
            pagging.TotaCount = totaCount;
            pagging.TotaSize = pageCount;
            pagging.PageData = list.OrderByDescending(x => x.CreateTime).Skip((request.pageIndex - 1) * request.pageSize).Take(request.pageSize).ToList();
            result.Data = pagging;
            return result;
        }
    }
}
